package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import DAO.RoleDAO;

/**
 * Servlet implementation class SaveRole
 */
public class SaveRole extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SaveRole() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/plain");
		response.setCharacterEncoding("utf-8");
		
		PrintWriter out = response.getWriter();
		String message[] = new String[39];
		
		String rolename = request.getParameter("name");
		String rolememo = request.getParameter("rdescribe");
		String js = request.getParameter("js");

		for(int i=0;i<message.length;i++)
			message[i]="0";
		
		Vector v = new Vector();
		int startlength=0,endlength,end;
		String id;
		endlength=js.indexOf(".");
		end=js.lastIndexOf(".");
	    id=js.substring(0,endlength);
		v.add(id);
		while(endlength!=end){
			startlength=js.indexOf(",",startlength+1);
			endlength=js.indexOf(".",endlength+1);
			id=js.substring(startlength+1, endlength);
			v.add(id);
		}
		int num;
		for(int i=0;i<v.size();i++){
			num = Integer.parseInt((String)v.get(i));
			message[num-1]="1";
		}
		
		for(int i=0;i<message.length;i++)
			System.out.println(".............................."+i+"  "+message[i]);
		
		RoleDAO role = new RoleDAO();
		role.AddRole(rolename, rolememo, message);
		out.print("{success:true,msg:'保存成功'}");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request,response);
	}

}
